import tinymce from 'tinymce'

import 'tinymce/themes/silver'
import 'tinymce/icons/default/icons'
import 'tinymce/plugins/advlist'
import 'tinymce/plugins/anchor'
import 'tinymce/plugins/autolink'
import 'tinymce/plugins/autosave'
import 'tinymce/plugins/code'
import 'tinymce/plugins/codesample'
import 'tinymce/plugins/directionality'
import 'tinymce/plugins/fullscreen'
import 'tinymce/plugins/hr'
import 'tinymce/plugins/insertdatetime'
import 'tinymce/plugins/link'
import 'tinymce/plugins/lists'
import 'tinymce/plugins/media'
import 'tinymce/plugins/nonbreaking'
import 'tinymce/plugins/noneditable'
import 'tinymce/plugins/pagebreak'
import 'tinymce/plugins/paste'
import 'tinymce/plugins/preview'
import 'tinymce/plugins/print'
import 'tinymce/plugins/save'
import 'tinymce/plugins/searchreplace'
import 'tinymce/plugins/tabfocus'
import 'tinymce/plugins/table'
import 'tinymce/plugins/image'
import 'tinymce/plugins/template'
import 'tinymce/plugins/textpattern'
import 'tinymce/plugins/visualblocks'
import 'tinymce/plugins/visualchars'
import 'tinymce/plugins/wordcount'

/** 作为独立使用时快速生成默认配置 */
export function getTinymceBaseConfig(): Record<string, any> {
  const publicPath = import.meta.env.VITE_PUBLIC_PATH
  const resource = `${publicPath}tinymce`

  const lang = 'zh_CN'

  const theme = 'oxide'

  const fontFormats = [
    ['方正小标宋_GBK', '方正小标宋_GBK'],
    ['方正仿宋_GBK', '方正仿宋_GBK'],
    ['方正黑体_GBK', '方正黑体_GBK'],
    ['方正楷体_GBK', '方正楷体_GBK']
  ]

  return {
    skin_url: `${resource}/skins/ui/${theme}`,
    branding: false,
    font_formats: fontFormats.map((f) => f.join('=')).join(';'),
    codesample_preview: true,
    default_link_target: '_blank',
    link_title: false,
    object_resizing: false,
    auto_focus: true,
    content_css: `${resource}/skins/content/default/content.css`,
    language_url: `${resource}/langs/${lang}.js`,
    toolbar:
      'bold italic underline strikethrough | fontsizeselect fontselect | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist | outdent indent blockquote | undo redo | link unlink image code | removeformat'
  }
}

const wordFontSizeSelect = [
  { title: '初号', pt: 42 },
  { title: '小初', pt: 36 },
  { title: '一号', pt: 26 },
  { title: '小一', pt: 24 },
  { title: '二号', pt: 22 },
  { title: '小二', pt: 18 },
  { title: '三号', pt: 16 },
  { title: '小三', pt: 15 },
  { title: '四号', pt: 14 },
  { title: '小四', pt: 12 },
  { title: '五号', pt: 10.5 },
  { title: '小五', pt: 9 },
  { title: '六号', pt: 7.5 },
  { title: '小六', pt: 6.5 },
  { title: '七号', pt: 5.5 },
  { title: '八号', pt: 5 }
]

export function initInlineTinymce(options?: Record<string, any>) {
  return tinymce.init({
    inline: true,
    menubar: false,

    fontsize_formats: wordFontSizeSelect
      .map((item) => `${item.title}=${item.pt}pt`)
      .join(' '),
    ...getTinymceBaseConfig(),
    ...options
  })
}
